package br.com.salao.dao;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import br.com.salao.bean.Servicos;
import br.com.salao.util.JpaUtil;



public class ServicosDaoImpl implements ServicosDao {

	EntityManager em = null;
	//Logger logger = Logger.getLogger(PaisDaoImpl.class.getName());

	public ServicosDaoImpl(){
		em = JpaUtil.getEntityManager();
	}

	public Servicos localizar(String descricao){

		EntityTransaction t = em.getTransaction();

		String atributo = "descricao";

		Servicos servicos = null;

		try {

			t.begin();
			Query query = em.createQuery("from Servicos where " + atributo + " like :param");
			query.setParameter("param", descricao);
			servicos = (Servicos) query.getSingleResult();
			t.commit();

		} catch (Exception e) {
			e.printStackTrace();

			if (t.isActive()) {
				t.rollback();
			}

		} finally {
			em.close();
		}
		return servicos;
	}
	public boolean inserir(Servicos servicos) {
		
		System.out.println("Passou aqui");

		//EntityManager em = new JpaUtil().getEntityManager("SalaoBeleza");

		em.getTransaction().begin();		

		try{

			//if(localizar(pais.getDescricao()) == null){
				em.persist(servicos);
				em.getTransaction().commit();
			//}
			return true;

		}catch (Exception e) {

			e.printStackTrace();
			return false;

		}finally{
			if(em != null){
				em.close();
			}
			JpaUtil.closeEntityMgrFactory();
		}
	}
	public Servicos find(int id){
		
		em.getTransaction().begin();
		Servicos servico = (Servicos)em.find(Servicos.class, id);
		em.getTransaction().commit();
		
		return servico;
	}

}
